package org.lex.ftp.ui.opendialog;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import org.lex.ftp.util.Resources;
import org.lex.net.Password;
import org.lex.net.ftp.FTPURL;
import org.lex.swing.widgets.EnhancedDialog;


public class OpenDialog extends EnhancedDialog {
	public static final int OkOption = 1;
	public static final int CancelOption = 0;
	private static final String OKAC = "ok";
	private static final String CancelAC = "cancel";
	private static final String DeleteAC = "delete";
	private static final Object[][] history = { { "svchost", new FTPURL("10.85.25.200", 21) }, { "1304", new FTPURL("10.64.130.23") },
			{ "fudan", new FTPURL("ftp.fudan.edu.cn") }, { "baozi", new FTPURL(new Password("list", "list".toCharArray()), "10.8.8.8") } };
	// ==============================================
	private JPanel pane = null;
	private JTabbedPane mainTabbedPane = null;
	private JPanel generalPanel = null;
	private JPanel advancedPanel = null;
	private JLabel histroyLabel = null;
	private JButton deleteButton = null;
	private JComboBox histroyComboBox = null;
	private JPanel blankPanel = null;
	private JPanel buttonsPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JCheckBox antiIdleCheckBox = null;
	private JCheckBox passiveCheckBox = null;
	private JPanel optionsPanel = null;
	private FTPURL account = null;
	private ButtonsActionHandler buttonsHandler = null;
	private ConnectionPane connectionPane = null;

	public OpenDialog(Window root) {
		super(root, "Connect to", true);
		initialize();
	}

	public FTPURL showOpenDialog() {
		this.showHistory();
		this.setVisible(true);
		return this.account;
	}

	public void clearAll() {
		this.getConnectionPane().clearAll();
	}

	private void showHistory() {
		int selected = histroyComboBox.getSelectedIndex();
		if (-1 == selected)
			return;
		FTPURL url = (FTPURL) history[selected][1];
		getConnectionPane().setUrl(url);
	}

	/**
	 * ����GUI
	 */
	private void initialize() {
		this.setSize(415, 260);
		this.setContentPane(getPane());
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				account = null;
				OpenDialog.this.setVisible(false);
			}
		});
		this.setDefaultButton(this.getOkButton());
	}

	private JPanel getPane() {
		if (pane == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setVgap(0);
			borderLayout.setHgap(0);
			pane = new JPanel();
			pane.setLayout(borderLayout);
			pane.add(getMainTabbedPane(), java.awt.BorderLayout.CENTER);
			pane.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
		}
		return pane;
	}

	private JTabbedPane getMainTabbedPane() {
		if (mainTabbedPane == null) {
			mainTabbedPane = new JTabbedPane();
			mainTabbedPane.addTab("General", null, getGeneralPanel(), null);
			mainTabbedPane.addTab("Advanced", null, getAdvancedPanel(), null);
		}
		return mainTabbedPane;
	}

	private JPanel getGeneralPanel() {
		if (generalPanel == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridwidth = 3;
			gridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.gridy = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridwidth = 3;
			gridBagConstraints8.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints8.weighty = 10.0D;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 200.0D;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.insets = new java.awt.Insets(11, 0, 5, 5);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints2.ipadx = 0;
			gridBagConstraints2.insets = new java.awt.Insets(11, 3, 5, 5);
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(11, 6, 5, 3);
			gridBagConstraints.gridy = 0;
			histroyLabel = new JLabel();
			histroyLabel.setText("Histroy");
			histroyLabel.setVerticalTextPosition(javax.swing.SwingConstants.CENTER);
			histroyLabel.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
			generalPanel = new JPanel();
			generalPanel.setLayout(new GridBagLayout());
			generalPanel.add(histroyLabel, gridBagConstraints);
			generalPanel.add(getDeleteButton(), gridBagConstraints2);
			generalPanel.add(getHistroyComboBox(), gridBagConstraints1);
			generalPanel.add(getBlankPanel(), gridBagConstraints8);
			generalPanel.add(getConnectionPane(), gridBagConstraints11);
		}
		return generalPanel;
	}

	private JPanel getAdvancedPanel() {
		if (advancedPanel == null) {
			BorderLayout borderLayout1 = new BorderLayout();
			borderLayout1.setHgap(0);
			advancedPanel = new JPanel();
			advancedPanel.setLayout(borderLayout1);
			advancedPanel.add(getOptionsPanel(), java.awt.BorderLayout.NORTH);
		}
		return advancedPanel;
	}

	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("");
			deleteButton.setIcon(Resources.getIcon(Resources.DeleteIcon));
			deleteButton.setPreferredSize(new java.awt.Dimension(22, 22));
			deleteButton.setActionCommand(DeleteAC);
			deleteButton.addActionListener(this.getButtonsHandler());
		}
		return deleteButton;
	}

	private JComboBox getHistroyComboBox() {
		if (histroyComboBox == null) {
			histroyComboBox = new JComboBox();
			for (int i = 0; i < history.length; i++)
				histroyComboBox.addItem(history[i][0]);
			histroyComboBox.setSelectedIndex(0);
			histroyComboBox.setPreferredSize(new java.awt.Dimension(32, 22));
			histroyComboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (ItemEvent.DESELECTED == e.getStateChange())
						return;
					showHistory();
				}
			});
		}
		return histroyComboBox;
	}

	private JPanel getBlankPanel() {
		if (blankPanel == null) {
			blankPanel = new JPanel();
		}
		return blankPanel;
	}

	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new JPanel();
			FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 12, 12);
			layout.setAlignOnBaseline(true);
			buttonsPanel.setLayout(layout);
			buttonsPanel.add(this.getOkButton());
			buttonsPanel.add(this.getCancelButton());
		}
		return buttonsPanel;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.setMnemonic(java.awt.event.KeyEvent.VK_O);
			okButton.setActionCommand(OKAC);
			okButton.addActionListener(this.getButtonsHandler());
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setMnemonic(java.awt.event.KeyEvent.VK_C);
			cancelButton.setActionCommand(CancelAC);
			cancelButton.addActionListener(this.getButtonsHandler());
		}
		return cancelButton;
	}

	private JCheckBox getAntiIdleCheckBox() {
		if (antiIdleCheckBox == null) {
			antiIdleCheckBox = new JCheckBox();
			antiIdleCheckBox.setSelected(true);
			antiIdleCheckBox.setText("Anti - Idle");
		}
		return antiIdleCheckBox;
	}

	private JCheckBox getPassiveCheckBox() {
		if (passiveCheckBox == null) {
			passiveCheckBox = new JCheckBox();
			passiveCheckBox.setSelected(true);
			passiveCheckBox.setText("Use Passive Mode");
		}
		return passiveCheckBox;
	}

	private JPanel getOptionsPanel() {
		if (optionsPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			optionsPanel = new JPanel();
			optionsPanel.setLayout(gridLayout);
			optionsPanel.add(getAntiIdleCheckBox(), null);
			optionsPanel.add(getPassiveCheckBox(), null);
		}
		return optionsPanel;
	}

	private ConnectionPane getConnectionPane() {
		if (connectionPane == null) {
			connectionPane = new ConnectionPane();
		}
		return connectionPane;
	}

	private ButtonsActionHandler getButtonsHandler() {
		if (null == buttonsHandler)
			buttonsHandler = new ButtonsActionHandler();
		return buttonsHandler;
	}

	class ButtonsActionHandler implements ActionListener {
		private String ac = null;
		private OpenDialog dialog = OpenDialog.this;

		public void actionPerformed(ActionEvent e) {
			ac = e.getActionCommand();
			if (OKAC == ac) {
				account = dialog.getConnectionPane().getUrl();
				dialog.clearAll();
				dialog.setVisible(false);
			} else if (CancelAC == ac) {
				dialog.clearAll();
				account = null;
				dialog.setVisible(false);
			}
		}
	}
}
